package com.guigu.service_user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.common_util.pojo.utils.Result;
import com.guigu.service_user.entity.TbAddress;
import com.guigu.service_user.entity.TbAreas;
import com.guigu.service_user.mapper.TbAreasMapper;
import com.guigu.service_user.service.ITbAreasService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 * 行政区域县区信息表 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2020-07-09
 */
@Service
public class TbAreasServiceImpl extends ServiceImpl<TbAreasMapper, TbAreas> implements ITbAreasService {

    @Override
    public Result findPage(Map<String, Object> searchMap, Integer page, Integer size) {
        QueryWrapper<TbAreas> wrapper = createExample(searchMap);
        //创建page对象
        Page<TbAreas> pageAlbum = new Page<>(page,size);
        baseMapper.selectPage(pageAlbum,wrapper);
        return Result.ok().data("row",pageAlbum.getRecords()).data("total",pageAlbum.getTotal());
    }
    // 根据查询条件封装模块对象
    private QueryWrapper<TbAreas> createExample(Map<String, Object> searchMap) {
        QueryWrapper<TbAreas> wrapper = new QueryWrapper<>();
        if(searchMap!=null) {
            if(searchMap.get("areaid")!=null)
                wrapper.eq("areaid",searchMap.get("areaid"));
            if(searchMap.get("area")!=null && !"".equals(searchMap.get("area")))
                wrapper.like("area",searchMap.get("area"));
            if(searchMap.get("cityid")!=null && !"".equals(searchMap.get("cityid")))
                wrapper.like("cityid",searchMap.get("cityid"));
        }
        return wrapper;
    }
}
